package com.example.myapplication.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.example.myapplication.network.api.Category
import com.example.myapplication.network.core.NetworkResult
import com.example.myapplication.ui.common.ErrorContent
import com.example.myapplication.ui.common.LoadingContent
import com.example.myapplication.ui.viewmodel.CategoryViewModel

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun CategoryScreen(viewModel: CategoryViewModel = viewModel()) {
    val categoriesState = viewModel.categoriesState.collectAsState()
    //定义当前选中的一级分类的所用
    var level1Index = remember { mutableIntStateOf(0) }

    when (val state = categoriesState.value) {
        is NetworkResult.Loading<*> -> LoadingContent()
        is NetworkResult.Error<*> -> ErrorContent()
        is NetworkResult.Success<List<Category>> -> {
            Row(
                modifier = Modifier.fillMaxSize(),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.Top
            ) {
                //左侧的一级分类
                LazyColumn(
                    modifier = Modifier
                        .width(100.dp)
                        .fillMaxHeight()
                        .background(color = Color(0xFFF8F8F8))
                ) {
                    itemsIndexed(state.data) { index,firstCategory ->
                        Text(
                            text = firstCategory.name,
                            fontSize = 18.sp,textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth().height(50.dp)
                                .background(color = if (level1Index.value==index) Color.White else Color(0xFFF8F8F8))
                                .wrapContentHeight(
                                    Alignment.CenterVertically)
                                .clickable(onClick = {
                                    level1Index.value = index
                                })
                        )
                    }
                }

                //二级分类、三级分类
                LazyColumn(
                    modifier = Modifier
                        .weight(1F)
                        .fillMaxHeight()
                        .background(color = Color.White),
                    verticalArrangement = Arrangement.spacedBy(16.dp)
                ) {
                    itemsIndexed(state.data.get(level1Index.value).children){index,secondCategory->
                        Column(
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Text(
                                modifier = Modifier.fillMaxWidth().height(50.dp).wrapContentHeight(
                                    Alignment.CenterVertically),
                                text = secondCategory.name,
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold
                            )

                            FlowRow(
                                modifier = Modifier.fillMaxWidth(),
                                maxItemsInEachRow = 3,
                                horizontalArrangement = Arrangement.spacedBy(10.dp),
                                verticalArrangement = Arrangement.spacedBy(20.dp)
                            ) {
                                secondCategory.children.forEachIndexed {index, item->
                                    Column(
                                        modifier = Modifier.weight(1F), //平分宽度
                                        horizontalAlignment = Alignment.CenterHorizontally
                                    ) {
                                        AsyncImage(
                                            model = item.imageUrl,
                                            contentDescription = "图片",
                                            modifier = Modifier.size(60.dp)
                                        )

                                        Text(item.name)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


}